package com.hbnu.service;

import com.hbnu.dao.UserDao;
import com.hbnu.entity.UserFile;
import com.hbnu.entity.User;
import com.hbnu.enums.Status;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.UUID;

import static com.hbnu.service.FunctionService.fileService;
import static com.hbnu.tools.FileTool.existsAndIsFolder;
import static com.hbnu.tools.FileTool.formatFileSize;
import static com.hbnu.tools.PrintTool.print;
import static com.hbnu.tools.PrintTool.printSet;
import static com.hbnu.web.Client.*;
import static com.hbnu.web.Client.user;


public class UserService extends AbstractFunctionService {
    private final UserDao userDao =new UserDao();
    public UserService(){
        cmdMap.put("set",this::set);
        cmdMap.put("size",this::size);
        cmdMap.put("exit",this::exit);

        oneMap.put("set1",this::set1);
        oneMap.put("set2",this::set2);
        oneMap.put("set3",this::set3);
        oneMap.put("setall",this::setall);
    }
    //退出
    private void exit() {
        print("Bye");
        fileService.close();
        System.exit(0);
    }
    //检查账号、密码
    public User check(String userName, String password) {
        return userDao.check(userName, password);
    }

    //使用先接受，后修改的方法
    public Map<Integer, UserFile> getUserFileMap( String uid, int fid) {
        return userDao.getUserFileMap( uid,fid);
    }

    //注册
    public void register(String userName, String pwd) {
        String uuid = UUID.randomUUID().toString();
        userDao.register(uuid,userName,pwd);
    }

    //更新登录时间
    public void updateTime(String uid) {
        userDao.updateTime(uid);
    }

    //检查是否存在
    public boolean checkUser(String user){
        return userDao.checkUser(user);
    }

    //查看设置
    private void set(){
        printSet();
    }
    public void set1(String path){
        if(set0("def_ClientPath",path))
            Status.def_ClientPath=properties.getProperty("def_ClientPath");
    }
    public void set2(String path){
        if(set0("ftf_SavePath",path))
            Status.ftf_SavePath=properties.getProperty("ftf_SavePath");
    }
    public void set3(String path){
        if(set0("http_SavePath",path))
            Status.http_SavePath=properties.getProperty("http_SavePath");
    }
    private boolean set0(String key ,String path){
        File file = new File(path);
        if(existsAndIsFolder(file)){
            return updateProperties(key,file);
        }
        return false;
    }
    //全部修改
    private void setall(String path){
        File file = new File(path);
        if(existsAndIsFolder(file)){
            if(updateProperties("def_ClientPath",file))
                Status.def_ClientPath=properties.getProperty("def_ClientPath");
            if(updateProperties("ftf_SavePath",file))
                Status.ftf_SavePath=properties.getProperty("ftf_SavePath");
            if(updateProperties("http_SavePath",file))
                Status.http_SavePath=properties.getProperty("http_SavePath");
        }
    }
    private boolean updateProperties(String key,File file){
        return updateProperties0(key,file.getAbsolutePath());
    }
    public boolean updateProperties0(String key,String value){
        properties.setProperty(key, value);
        // 保存修改后的配置
        try (OutputStream output = new FileOutputStream(configPath)) {
            properties.store(output, "地址配置文件");
            return true;
        } catch (IOException e) {
            System.err.println("保存配置文件失败: " + e.getMessage());
            return false;
        }
    }
    //查看大小
    private void size(){
        String size = formatFileSize(user.getSize());
        String space = formatFileSize(user.getSpace());
        System.out.printf("存储空间(%.2f%%): %s/%s\n",user.getSize()*100/(double)user.getSpace(), size,space);
    }

}
